package com.xctech.yace.swing;

import com.xctech.yace.enumeration.KafkaRoleType;
import com.xctech.yace.util.DirUtil;
import com.xctech.yace.util.KafkaKerberosUtil;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import static com.xctech.yace.util.KafkaKerberosUtil.getKafkaSaslPlainConfMap;

public abstract class FatherSwing {

    public static Properties consumerPropties(){
        String filePath = DirUtil.getOrgPath()+"conf/kafkaconsumer.properties";
        Properties props = new Properties();
        Properties newProps = new Properties();
        try {
            props.load(new FileInputStream(filePath));
            Map<String, String> map = getKafkaSaslPlainConfMap(props, KafkaRoleType.CONSUMER);
            if(map != null){
                for(Map.Entry<String,String> entry:map.entrySet()){
                    String key = entry.getKey();
                    String value = entry.getValue();
                    props.setProperty(key,value);
                }
            }else{
                String mechanism = KafkaKerberosUtil.mechanism;
                String protocol = KafkaKerberosUtil.protocol;
                for (String field:props.stringPropertyNames()){
                    if (!(mechanism.equals(field) || protocol.equals(field))) {
                        newProps.setProperty(field,props.getProperty(field,""));
                    }
                }
                props = newProps;
            }
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return props;
    }

    public static Properties productPropties(){
        String filePath = DirUtil.getOrgPath()+"conf/kafka.properties";
        Properties props = new Properties();
        try {
            props.load(new FileInputStream(filePath));
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return props;
    }

    public static Properties dbPropties(){
        String filePath = DirUtil.getOrgPath()+"conf/db.properties";
        Properties props = new Properties();
        try {
            props.load(new FileInputStream(filePath));
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return props;
    }

    public static Properties vimDbPropties(){
        String filePath = DirUtil.getOrgPath()+"conf/cleardb.properties";
        Properties props = new Properties();
        try {
            props.load(new FileInputStream(filePath));
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return props;
    }

}
